<?php
namespace App\Models;

class HdMoLog extends Base {

    public function initialize() {
        parent::initialize();
    }

    public function getSource() {
        return 'hd_mo_log';
    }

    private function t() {
        return 'App\Models\HdMoLog';
    }

    /**
     * 插入数据
     *
     * @param array $data
     *
     * @return false|int
     */
    public function add($data) {
        try {
            if($this->db()->insertAsDict($this->getSource(), $data) !== false) {
                return $this->db()->lastInsertId();
            }
        } catch(\PDOException $ex) {
            $this->errorLog('INSERT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 更新数据
     *
     * @param array $data
     *
     * @return bool
     */
    public function edit($data) {
        try {
            return $this->db()->updateAsDict($this->getSource(), $data, sprintf("id=%d LIMIT 1", intval($data['id'])));
        } catch(\PDOException $ex) {
            $this->errorLog('UPDATE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 通过msgId获取当前上行信息
     *
     * @param $msgId
     *
     * @return array|false
     */
    public function getByMtMsgId($msgId) {
        $query = $this->select('*')->from($this->t())->where('mt_msg_id=:mt_msg_id:', ['mt_msg_id' => $msgId])->limit(1);
        try {
            $ret = $query->getQuery()->execute();
            if($ret != false) {
                $ret = $ret->toArray();
                if(is_array($ret) && isset($ret[0])) {
                    return $ret[0];
                }
            }
        } catch(\PDOException $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }
}